<template>
  <common_print_dialog singlePrintNumber ref="common_print_dialog" :businessType="businessType" v-bind="$attrs"
    v-on="$listeners" :tableModel="models" :printUrl="printUrl" :printHandler="printHandler" :previewUrl="previewUrl"
    :previewHandler="previewHandler" :previewMergeUrl="previewMergeUrl" :previewMergeHandler="previewMergeHandler" />
</template>

<script>
import common_print_dialog from './common_print_dialog.vue'
import role_system_mixin from '@/mixin/role_system_mixin';

export default {
  components: { common_print_dialog },
  mixins: [role_system_mixin,],

  data() {
    return {
      printUrl: null,
      previewUrl: null,
      previewMergeUrl: null,
      models: []
    }
  },
  props: {

    itemKey: {
      type: String,
      default: 'skuId'
    },
    businessType: {
      type: String,
      default: 'SKU'
    },
  },
  created() {
    if (this.businessType == 'SKU') {
      this.models = [
        { key: 'skuName', label: 'label_sku_name', 'width': 120, 'show-overflow-tooltip': true, },
        { key: 'skuBarcode', label: 'label_sku_code', 'min-width': 120, 'show-overflow-tooltip': true, },
        { key: 'skuOnlyCode', label: 'label_sku_only_code', 'min-width': 150, },
      ]
    } else if (this.businessType == 'AREA') {
      this.models = [
        { key: 'code', label: 'label_warehouse_area_code', 'min-width': 180, }
      ]
    } else if (this.businessType == 'CONTAINER') {
      this.models = [
        { key: 'code', label: 'label_container_code', 'min-width': 180, }
      ]
    } else if (this.businessType == 'LOCATION') {
      this.models = [
        { key: 'code', label: 'label_warehouse_location_code', 'min-width': 180, }
      ]
    }
    if (this.isCustomerSystem) {
      this.printUrl = "/mms-api/client/MmsSkuInfoClientRpc/skuPrint.json"
      this.previewUrl = "/mms-api/client/MmsSkuInfoClientRpc/skuPreviewDown.json"
      this.previewMergeUrl = "/mms-api/client/MmsSkuInfoClientRpc/skuBatchMerge.json"
    } else if (this.isOmsSystem) {

      this.printUrl = "/mms-api/sku/MmsSkuInfoRpc/skuPrint.json"
      this.previewUrl = "/mms-api/sku/MmsSkuInfoRpc/skuPreviewDown.json"
      this.previewMergeUrl = "/mms-api/sku/MmsSkuInfoRpc/skuBatchMerge.json"
    } else if (this.isWmsSystem) {

      this.printUrl = "/wms-api/print/PrintRpc/baseInfoPrint.json"
      this.previewUrl = "/wms-api/print/PrintRpc/previewPrintBase.json"
      this.previewMergeUrl = '/wms-api/print/PrintRpc/baseInfoMergePdf.json'
    }
  },
  methods: {
    printHandler(data, rows) {
      if (this.isMainSystem) {
        data.printItemVos = rows.map(row => {
          return {
            templateId: data.templateId,
            skuId: row[this.itemKey],
            fileType: data.fileType,
            printNum: row.printNum,
          }
        });
      } if (this.isWmsSystem) {
        data.wmsPrintItemVos = rows.map(row => {
          return {
            type: data.type,
            templateId: data.templateId,
            itemId: row[this.itemKey],
            fileType: data.fileType,
            printNum: row.printNum,
          }
        });
      }
      return data
    },
    previewHandler(data, row) {
      if (this.isMainSystem) {
        data.skuId = row[this.itemKey];
        data.printNum = row.printNum;
      } if (this.isWmsSystem) {
        data.itemId = row[this.itemKey];
        data.printNum = row.printNum;
      }
      return data
    },

    previewMergeHandler(data, rows) {
      if (this.isMainSystem) {
        data.printItemVos = rows.map(row => {
          return {
            templateId: data.templateId,
            skuId: row[this.itemKey],
            fileType: data.fileType,
            printNum: row.printNum,
          }
        });
      } if (this.isWmsSystem) {
        data.wmsPrintItemVos = rows.map(row => {
          return {
            type: data.type,
            templateId: data.templateId,
            itemId: row[this.itemKey],
            fileType: data.fileType,
            printNum: row.printNum,
          }
        });
      }

      return data
    },
    show(rows, option) {
      this.$refs.common_print_dialog.show(rows, option)
    },
    printDefaultOption(option){
      this.$refs.common_print_dialog.printDefaultOption(option)
    }

  }
}
</script>
